package com.xhh.nphm.system.service.impl;

import com.xhh.nphm.system.domain.MakeTag;
import com.xhh.nphm.system.domain.MakeTagUser;
import com.xhh.nphm.system.domain.TagLibrary;
import com.xhh.nphm.system.mapper.MakeTagDao;
import com.xhh.nphm.system.mapper.MakeTagUserDao;
import com.xhh.nphm.system.mapper.TagDao;
import com.xhh.nphm.system.service.ITagService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class TagServiceImpl implements ITagService {

    @Resource
    private TagDao tagDao;

    @Resource
    private MakeTagDao makeTagDao;

    @Resource
    private MakeTagUserDao makeTagUserDao;

    @Override
    public List<TagLibrary> selectTagListJson(TagLibrary tagLibrary) {
        return tagDao.selectTagListJson(tagLibrary);
    }

    @Override
    public List<Map<String, Object>> selectCommonTag() {
        return tagDao.selectCommonTag();
    }

    @Override
    public List<String> autoComplete() {
        return tagDao.autoComplete();
    }

    @Override
    public boolean saveTag(TagLibrary tagLibrary) {
        return tagDao.insert(tagLibrary) > 0;
    }

    @Override
    public TagLibrary selectTagByTagNo(Long tagNo) {
        return tagDao.selectById(tagNo);
    }

    @Override
    public boolean updateTag(TagLibrary tagLibrary) {
        return tagDao.updateById(tagLibrary) > 0;
    }

    @Override
    public boolean removeTag(Long tagNo) {
        return tagDao.deleteById(tagNo) > 0;
    }

    @Override
    public List<Map<String, Object>> selectTelExistTag(String telWfId, String deptId, String userId) {
        return makeTagUserDao.selectTelExistTag(telWfId,deptId,userId);
    }

    @Override
    public List<Map<String, Object>> selectMailExistTag(String mailWfId, String deptId, String userId) {
        return makeTagUserDao.selectMailExistTag(mailWfId,deptId,userId);
    }

    @Override
    public Long selectTagByTagName(String tagName) {
        return tagDao.selectByTagName(tagName);
    }

    @Override
    public boolean saveTelMakeTag(MakeTag makeTag) {
        if(makeTagDao.updateByTagNoT(makeTag.getRecordId(),makeTag.getTagPkid()) > 0){
            return true;
        }
        return makeTagDao.saveTelMakeTag(makeTag) > 0;
    }

    @Override
    public boolean saveMailMakeTag(MakeTag makeTag) {
        if(makeTagDao.updateByTagNoM(makeTag.getRecordId(),makeTag.getTagPkid()) > 0){
            return true;
        }
        return makeTagDao.saveMailMakeTag(makeTag) > 0;
    }

    @Override
    public boolean saveTelMakeTagByUser(MakeTagUser makeTagUser) {
        //存在更新
        if(makeTagUserDao.updateByTagNoTel(makeTagUser.getRecordId(),makeTagUser.getTagPkid(), makeTagUser.getDeptId()) > 0){
            return true;
        }
        return makeTagUserDao.saveTelMakeTagByUser(makeTagUser) > 0;
    }

    @Override
    public boolean saveMailMakeTagByUser(MakeTagUser makeTagUser) {
        //存在更新
        if(makeTagUserDao.updateByTagNoMail(makeTagUser.getRecordId(),makeTagUser.getTagPkid(), makeTagUser.getDeptId()) > 0){
            return true;
        }
        return makeTagUserDao.saveMailMakeTagByUser(makeTagUser) > 0;
    }

    @Override
    public boolean deleteWFTag(MakeTag makeTag, String deptId) {
        if (makeTag.getType() == 1) {
            makeTagUserDao.deleteByTagNoTel(makeTag.getRecordId(), makeTag.getTagPkid(),deptId);
            return makeTagDao.deleteTByRecordIdOrTagId(makeTag.getRecordId(),makeTag.getTagPkid()) > 0;
        }
        makeTagUserDao.deleteByTagNoMail(makeTag.getRecordId(), makeTag.getTagPkid(),deptId);
        return makeTagDao.deleteMByRecordIdOrTagId(makeTag.getRecordId(),makeTag.getTagPkid()) > 0;
    }
}
